import conf from 'api/conf'
import axios from 'axios';
import qs from 'qs';
import storage from 'storage'
import Vue from 'vue'

const io = {
    configUrls: function () {
        // 登陆
        this.login = conf.baseApiPath + '/api/login'
        this.updatePass = conf.baseApiPath + '/api/updatePass'
        // 用户
        this.detailAuthUser = conf.baseApiPath + '/authUser/detailAuthUser'
        this.updateAuthUser = conf.baseApiPath + '/authUser/updateAuthUser'
        this.userStuStatus = conf.baseApiPath + '/authUser/userStuStatus'
        // 词典
        this.getWordRepoList = conf.baseApiPath + '/api/wordRepo/getWordRepoList'
        this.getUserStatistics = conf.baseApiPath + '/api/wordRepo/getUserStatistics'
        // 关卡
        this.getUnitList = conf.baseApiPath + '/api/wordUnit/getUnitList'
        this.passUnit = conf.baseApiPath + '/api/wordUnit/passUnit'
        // 单词
        this.getWordById = conf.baseApiPath + '/api/word/getWordById'
        this.findWordPage = conf.baseApiPath + '/api/word/findWordPage'
        this.getAudio = conf.baseApiPath + '/api/word/getAudio'
        this.getYDAudio = conf.baseApiPath + '/api/word/getYDAudio'
    },
    getHeaders: function () {
        const accessToken = storage.getAccessToken();
        return {
            accessToken
        }
    },
    post: async function (url, data, success, fail, timeout) {
        try {
            data = { ...data }
            let formdata = qs.stringify(data);
            let res = await axios({
                url: url,
                method: 'POST',
                data: formdata,
                dataType: 'json',
                timeout: timeout || 30000,
                headers: { accessToken: this.getHeaders().accessToken }
            });
            if (res.data.code === '3001') {
                storage.removeAccessToken()
                storage.removeCurrentUserInfo()
                // console.log('url',document.location)
                document.location.href =document.location.host
                // Vue.prototype.$router.push('/login')
            }
            if (!res.data.success) {
                MessageBox('提示', res.data.desc);
            }
            return res;
        } catch (err) {
            console.log(err)
            MessageBox('提示', '网络异常');
        }
    },
    get: async function (url, data, success, fail, timeout) {
        try {
            data = { accessToken: this.getHeaders().accessToken, ...data }
            let formdata = qs.stringify(data);
            let res = await axios(url + "?" + formdata, {
                dataType: 'json',
                timeout: timeout || 30000
            });
            if (res.data.code === '3001') {
                storage.removeAccessToken()
                storage.removeCurrentUserInfo()
                Vue.prototype.$router.push('/login')
            }
            if (!res.data.success && !res.data.uptoken) {
                Vue.prototype.$notify.error({
                    title: '错误',
                    message: res.data.desc
                });
            }
            return res;
        } catch (err) {
            Vue.prototype.$notify.error({
                title: '错误',
                message: err.name
            })
            return
        }
    },

    getScripts: function (urls, done) {
        var $scripts = $.map(urls, function (url) {
            return $.cachedScript(url)
        });
        $scripts.push($.Deferred(function (deferred) {
            $(deferred.resolve);
        }));
        $.when.apply($, $scripts).done(done)
    },

}

io.configUrls()

export default io
